/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author matsu
 */
public class FileUtils {

    public static String md5File(File file) throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream(file);
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
    }
    
    public static String readFileAsString(File file) throws IOException {
        URI uri = file.toURI();
        byte[] bytes = null;
        bytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(uri));
        return new String(bytes);

    }

    public static void writeStringToFile( String content, File path) throws IOException {
        BufferedWriter writer =
                Files.newBufferedWriter(path.toPath(), Charset.defaultCharset(),
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
        writer.write(content, 0, content.length());
        writer.flush();
    }
}
